{extend name="index/iframe" /}

{block name="content"}
<form class="layui-form layui-form-my" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">费用类型</label>
            <div class="layui-input-inline">
                <select name="pay_type" lay-filter="pay_type" id="pay_type" lay-verify="required">
                    <option value="1">CPS和联运</option>
                    <option value="2">服务器费用</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结账类型</label>
            <div class="layui-input-inline">
                <select name="balance_type" lay-filter="balance_type" id="balance_type" lay-verify="required">
                    <option value="">全部</option>
                    {foreach $Think.config.balance_type as $k=>$v}
                    <option value="{$k}">{$v}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">选择游戏</label>
            <div class="layui-input-inline">
                {MyTag:game type='manage' /}
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">对账日期</label>
            <div class="layui-input-inline" id="audit_time">
                <input type="text" name="date" class="layui-input" lay-verify="required" id="date" placeholder="yyyy-MM-dd - yyyy-MM-dd"  lay-verify="date" autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">发票类型</label>
            <div class="layui-input-inline">
                <select name="invoice_type" lay-filter="invoice_type" id="invoice_type" lay-verify="required">
                    <option value="">全部</option>
                    {volist name="$Think.config.cp_invoice_type" id="vo"}
                    <option value="{$vo.type}" rate="{$vo.rate}">{$vo.name}</option>
                    {/volist}
                </select>
            </div>
        </div>
    </div>
    <hr>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">CPS充值</label>
            <div class="layui-input-inline">
                <input type="tel" value="0.00" name="cps_fixed_price" id="cps_fixed_price" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input layui-disabled" disabled>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">联运充值</label>
            <div class="layui-input-inline">
                <input type="text" value="0.00" name="sdk_fixed_price" id="sdk_fixed_price" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input layui-disabled" disabled>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">CPS充值</label>
            <div class="layui-input-inline">
                <input type="tel" value="0.00" name="cps_pay_price" id="cps_pay_price" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">联运充值</label>
            <div class="layui-input-inline">
                <input type="text" value="0.00" name="sdk_pay_price" id="sdk_pay_price" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">渠道费率</label>
            <div class="layui-input-inline">
                <input type="text" value="0" name="channel_rate" id="channel_rate" lay-verify="required" placeholder="%" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">税率</label>
            <div class="layui-input-inline">
                <input type="text" value="0" name="tax_rate" id="tax_rate" lay-verify="required" placeholder="%" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结算比例</label>
            <div class="layui-input-inline">
                <input type="text" value="0" name="pay_rate" id="pay_rate" lay-verify="required" placeholder="%" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">CPS结算</label>
            <div class="layui-input-inline">
                <input type="tel" value="0.00" name="cps_total_price" id="cps_total_price" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">联运结算</label>
            <div class="layui-input-inline">
                <input type="text" value="0.00" name="sdk_total_price" id="sdk_total_price" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">服务器费用</label>
            <div class="layui-input-inline">
                <input type="text" value="0.00" disabled name="server_price" id="server_price" placeholder="元" autocomplete="off" class="layui-input layui-disabled">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">合计</label>
            <div class="layui-input-inline">
                <input type="text" value="0.00" name="all_total_price" id="all_total_price" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn my-btn" lay-submit="" lay-filter="submitBtn" >立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
{/block}

{block name="css"}{/block}

{block name="js"}
<script>
    layui.use(['form', 'layedit', 'laydate', 'upload', 'element'], function(){
        var form = layui.form
            ,$ = layui.$
            ,layer = layui.layer
            ,laydate = layui.laydate;

        //服务器费用
        form.on('select(pay_type)', function(data){
            var pay_type = data.value;
            console.dir(pay_type);
            if(pay_type==1){
                $('#cps_pay_price').removeAttr("disabled").removeClass('layui-disabled');
                $('#sdk_pay_price').removeAttr("disabled").removeClass('layui-disabled');
                $('#cps_total_price').removeAttr("disabled").removeClass('layui-disabled');
                $('#sdk_total_price').removeAttr("disabled").removeClass('layui-disabled');
                $('#server_price').attr("disabled","disabled").addClass('layui-disabled');
            }else{
                $('#cps_pay_price').attr("disabled","disabled").addClass('layui-disabled');
                $('#sdk_pay_price').attr("disabled","disabled").addClass('layui-disabled');
                $('#cps_total_price').attr("disabled","disabled").addClass('layui-disabled');
                $('#sdk_total_price').attr("disabled","disabled").addClass('layui-disabled');
                $('#server_price').removeAttr("disabled").removeClass('layui-disabled');

                //日期
                laydate.render({
                    elem: '#date'
                    ,type: 'date'
                    ,range: true
                    ,min: '2016-01-01'
                    ,max: 0
                });
            }
        });


        //选择游戏
        form.on('select(game_id)', function(data){
            var game_id = data.value
                ,date = $('input[name="date"]').val()
                ,invoice_type = $('#invoice_type').val()
                ,balance_type = $('#balance_type').val();

            if(game_id && balance_type){
                time(game_id, balance_type);
            }

            if(game_id && date && invoice_type && balance_type){
                select(game_id, date, invoice_type, balance_type);
            }
        });

        //发票类型
        form.on('select(invoice_type)', function(data){
            var game_id = $('#game_id').val()
                ,date = $('input[name="date"]').val()
                ,invoice_type = data.value
                ,balance_type = $('#balance_type').val();
            if(game_id && date && invoice_type && balance_type){
                select(game_id, date, invoice_type, balance_type);
            }
        });

        //结账类型
        form.on('select(balance_type)', function(data){
            var game_id = $('#game_id').val()
                ,date = $('input[name="date"]').val()
                ,invoice_type = $('#invoice_type').val()
                ,balance_type = data.value;

            if(game_id && balance_type){
                time(game_id, balance_type);
            }

            if(game_id && date && invoice_type && balance_type){
                select(game_id, date, invoice_type, balance_type);
            }
        });

        //获取对账时间
        function time(game_id, balance_type){
            if($('#pay_type').val()==2){
                return false;
            }

            var load = layer.load();
            form.render();

            $.post('{:url("AuditCpMng/time")}',{game_id: game_id, balance_type: balance_type}, function(json){
                layer.close(load);
                if(json.code==0){
                    var  d = new Date().getTime()
                        , html = '<input type="text" name="date" class="layui-input" lay-verify="required" id="date'+d+'" placeholder="yyyy-MM-dd - yyyy-MM-dd"  lay-verify="date" autocomplete="off">';
                    $('#audit_time').html(html);
                    laydate.render({
                        elem: '#date'+d
                        ,type: 'date'
                        ,range: true
                        ,min: json.data
                        ,max: 0
                        ,done: function(value, date, endDate){
                            var game_id = $('#game_id').val()
                                ,date = value
                                ,invoice_type = $('#invoice_type').val()
                                ,balance_type = $('#balance_type').val();
                            if(game_id && date && invoice_type && balance_type){
                                select(game_id, date, invoice_type, balance_type);
                            }
                        }
                    });
                }else{
                    layer.msg(json.msg, {icon: 0, time: 1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        }

        //查询数据
        function select(game_id, date, invoice_type, balance_type){
            if($('#pay_type').val() == 2){
                return false;
            }
            var load = layer.load();
            $.post('{:url("AuditCpMng/select")}',{game_id: game_id, date: date, invoice_type: invoice_type, balance_type: balance_type}, function(json){
                layer.close(load);
                if(json.code==0){
                    $('#cps_pay_price').val(json.data.cps_pay_price);
                    $('#sdk_pay_price').val(json.data.sdk_pay_price);
                    $('#cps_fixed_price').val(json.data.cps_fixed_price);
                    $('#sdk_fixed_price').val(json.data.sdk_fixed_price);
                    $('#channel_rate').val(json.data.channel_rate);
                    $('#tax_rate').val(json.data.tax_rate);
                    $('#pay_rate').val(json.data.pay_rate);
                    $('#cps_total_price').val(json.data.cps_total_price);
                    $('#sdk_total_price').val(json.data.sdk_total_price);
                    $('#all_total_price').val(json.data.all_total_price);
                }else{
                    layer.msg(json.msg, {icon: 0, time: 1000});
                    $('input[name="date"]').val('');
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        }

        //input离开事件
        $('#cps_pay_price').blur(function(){
            var cps_pay_price = $('#cps_pay_price').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,sdk_total_price = $('#sdk_total_price').val()
                ,cps_total_price = (cps_pay_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2)
                ,all_total_price = (parseFloat(cps_total_price)+parseFloat(sdk_total_price)).toFixed(2);
            $('#cps_total_price').val(cps_total_price);
            $('#all_total_price').val(all_total_price);
        })
        $('#sdk_pay_price').blur(function(){
            var sdk_pay_price = $('#sdk_pay_price').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,cps_total_price = $('#cps_total_price').val()
                ,sdk_total_price = (sdk_pay_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2)
                ,all_total_price = (parseFloat(cps_total_price)+parseFloat(sdk_total_price)).toFixed(2);
            $('#sdk_total_price').val(sdk_total_price);
            $('#all_total_price').val(all_total_price);
        })
        $('#channel_rate').blur(function(){
            var cps_pay_price = $('#cps_pay_price').val()
                ,sdk_pay_price = $('#sdk_pay_price').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,cps_total_price = (cps_pay_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2)
                ,sdk_total_price = (sdk_pay_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2)
                ,all_total_price = (parseFloat(cps_total_price)+parseFloat(sdk_total_price)).toFixed(2);

            $('#cps_total_price').val(cps_total_price);
            $('#sdk_total_price').val(sdk_total_price);
            $('#all_total_price').val(all_total_price);
        })
        $('#tax_rate').blur(function(){
            var cps_pay_price = $('#cps_pay_price').val()
                ,sdk_pay_price = $('#sdk_pay_price').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,cps_total_price = (cps_pay_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2)
                ,sdk_total_price = (sdk_pay_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2)
                ,all_total_price = (parseFloat(cps_total_price)+parseFloat(sdk_total_price)).toFixed(2);

            $('#cps_total_price').val(cps_total_price);
            $('#sdk_total_price').val(sdk_total_price);
            $('#all_total_price').val(all_total_price);
        })
        $('#pay_rate').blur(function(){
            var cps_pay_price = $('#cps_pay_price').val()
                ,sdk_pay_price = $('#sdk_pay_price').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,cps_total_price = (cps_pay_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2)
                ,sdk_total_price = (sdk_pay_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2)
                ,all_total_price = (parseFloat(cps_total_price)+parseFloat(sdk_total_price)).toFixed(2);
            $('#cps_total_price').val(cps_total_price);
            $('#sdk_total_price').val(sdk_total_price);
            $('#all_total_price').val(all_total_price);
        })
        $('#cps_total_price').blur(function(){
            var cps_total_price = $('#cps_total_price').val()
                ,sdk_total_price = $('#sdk_total_price').val()
                ,all_total_price = (parseFloat(cps_total_price)+parseFloat(sdk_total_price)).toFixed(2);
            $('#all_total_price').val(all_total_price);
        })
        $('#sdk_total_price').blur(function(){
            var cps_total_price = $('#cps_total_price').val()
                ,sdk_total_price = $('#sdk_total_price').val()
                ,all_total_price = (parseFloat(cps_total_price)+parseFloat(sdk_total_price)).toFixed(2);
            $('#all_total_price').val(all_total_price);
        })
        $('#server_price').blur(function(){
            var server_price = $('#server_price').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,all_total_price = (server_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2);
            $('#all_total_price').val(all_total_price);
        })
        form.on('submit(submitBtn)', function(data){
            var str = '';
            layer.msg('生成中....', {icon: 1, time: 2000});
            for(key in data.field){
                str += key + '=' + data.field[key] + '&';
            }
            window.location.href = '{:url("AuditCpMng/add")}?'+str.substring(0, str.length-1);
            setTimeout(function(){
                window.location.reload();
            }, 2000);
            return false;
        });
    });
</script>
{/block}